<?php
	require_once 'environment.php';
	
	/**
	* The following mess contains code required to moderate most events.
	*/
	
	//Admins only!
	if(!$acc->isLogged()) die(change_site("account.php"));
	//Sub menus
	$temp->addSubMenu("Add Event", "modEvent.php?action=addEvent");
	$temp->addSubMenu("Edit Event", "modEvent.php?action=editEvent");
	$temp->addSubMenu("Delete Event", "modEvent.php?action=delEvent");
	$temp->addSubMenu("Moderate Events", "modEvent.php?action=modEvent");
	
	$_event = new Event($acc, $db);
	
	function throwErr($where, $what, $whereElse = "") {
		$what = urlencode($what);
		print change_site("modEvent.php?action={$where}&error={$what}{$whereElse}");
		die();
	}
	
	$action = fixArr($_GET, "action");
	switch($action){
		default:
			$temp->generate();
			print "You must select an action.";
			break;
		case("addEvent"):
			$temp->setTab(1, 0);
			$temp->currentPage("Home >> Events >> Add Event");
			$temp->generate();
			if(!fixArr($_POST, "month")) {
				$months = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
				print "<form action=\"\" method=\"POST\">\n";
				print "<span style=\"color: red;\">".(fixArr($_GET, "error") ? fixArr($_GET, "error")."<br />" : "")."</span>";
				print "<table width=55% align=center><tr><td style=\"width:95px;\"><label>Event Name: </td>";
				print "<td><input type=\"text\" size=35 maxlength=\"250\" name=\"eventName\" /></label></td></tr>";
				print "<tr><td><label>Description: </td>";
				print "<td><input type=\"text\" size=35 maxlength=\"250\" name=\"eventDesc\" /></label></td></tr>";
				print "<tr><td><label>Place: </td>";
				print "<td><input type=\"text\" size=35 maxlength=\"250\" name=\"eventLoc\" /></label></td></tr>";
				print "<tr><td>Date: </td><td><select name=\"month\">\n";
				for($i = 0; $i < 12; $i++) print "<option".($months[$i] == date('F') ? " selected=\"selected\" " : "").">{$months[$i]}</option>\n";
				print "</select>\n";
				print "<select name=\"day\">\n";
				for($i = 1; $i <= 31; $i++) print "<option".($i == date('j') ? " selected=\"selected\" " : "").">{$i}</option>\n";
				print "</select>\n";
				print "<select name=\"year\">\n";
				for($i = date('Y'); $i <= date('Y') + 10; $i++) print "<option>{$i}</option>\n";
				print "</select></td></tr>";
				print "<tr><td>Time (HH:MM): </td>";
				print "<td><input type=\"text\" maxlength=\"2\" size=\"1\" name=\"hour\" />&nbsp;:&nbsp;<input type=\"text\" maxlength=\"2\" size=\"1\" name=\"minute\" />\n<select name=\"ap\">\n<option>AM</option>\n<option>PM</option>\n</select>\n</td></tr>";
				print "<tr><td></td><td><br /><input type=\"submit\" value=\"Add Event\" /> <input type=\"reset\" value=\"Reset\" /></td></tr>";
				print "</table></form>";
			}else{
				foreach($_POST as $k => $data){
					$_POST[$k] = trim($data);
					if(empty($_POST[$k])) throwErr("addEvent", "All fields are required.");
				}
				$eventName = trim(fixArr($_POST, "eventName"));
				$eventDesc = trim(fixArr($_POST, "eventDesc"));
				$eventLoc = fixArr($_POST, "eventLoc");
				$month = fixArr($_POST, "month");
				$day = fixArr($_POST, "day");
				$year = fixArr($_POST, "year");
				if(fixArr($_POST, "hour") > 12) throwErr("addEvent", "There are only 12 hours in a day/night. Select PM if you want to use evening times.");
				if(fixArr($_POST, "minute") > 59) throwErr("addEvent", "There are only 0-59 minutes in 1 hour.");
				$time = fixArr($_POST, "hour").":".fixArr($_POST, "minute")." ".fixArr($_POST, "ap");
				$eventTime = strtotime($month." ".$day.", ".$year." ".$time);
				if($eventTime < time()) throwErr("addEvent", "The date you picked is in the past.");
				$_event->postEvent($eventName, $eventDesc, $eventTime, $eventLoc);
			   print "Event added successfully.";
			}
			break;
		case("editEvent"):
			$temp->setTab(1, 1);
			$temp->currentPage("Home >> Events >> Edit Event");
			$temp->generate();
			if(!fixArr($_GET, "eventID")){
				$sql = $db->Execute("SELECT * FROM `monster`.`event` WHERE `Date` > ? AND `Approved` = \"1\" ORDER BY `event`.`Date` ASC", array(time()));
				if($sql->RecordCount() > 0){
					print "<span style=\"color: red;\">".(fixArr($_GET, "error") ? fixArr($_GET, "error")."<br />\n" : "")."</span>\n<b>List of Events</b><br />";
					while($row = $sql->FetchRow()){
						print "<br />\nEvent Name: <a href=\"modEvent.php?action=editEvent&eventID={$row['AutoInc']}\">{$row['Title']}</a><br />\n";
						print "<b>Date and Time: </b>".date('F jS, Y g:i A', $row['Date'])."<br />\n";
						print "<b>Description: </b>{$row['Description']}<br />\n";
					}
				}
			}else{
				$event = $_event->getEventByID(fixArr($_GET, "eventID"));
				if($event){
					if(!fixArr($_POST, "month")){
						$months = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
						print "<form action=\"\" method=\"POST\">\n";
						print "<span style=\"color: red;\">".(fixArr($_GET, "error") ? fixArr($_GET, "error")."<br />" : "")."</span>";
						print "<table width=\"55%\" align=\"center\"><tr><td style=\"width:95px;\"><label>Event Name: </label></td>\n";
						print "<td><input type=\"text\" name=\"eventName\" value=\"{$event['Title']}\" /></td></tr>\n";
						print "<tr><td><label>Description: </label></td><td><input type=\"text\" name=\"eventDesc\" value=\"{$event['Description']}\" /></td></tr>\n";
						print "<tr><td><label>Place: </label></td><td><input type=\"text\" name=\"eventLoc\" value=\"{$event['Place']}\" /></td></tr>\n";
						print "<tr><td>Date: </td><td><select name=\"month\">\n";
						for($i = 0; $i < 12; $i++) print "<option".($months[$i] == date('F', $event['Date']) ? " selected=\"selected\" " : "").">{$months[$i]}</option>\n";
						print "</select>\n";
						print "<select name=\"day\">\n";
						for($i = 1; $i <= 31; $i++) print "<option".($i == date('j', $event['Date']) ? " selected=\"selected\" " : "").">{$i}</option>\n";
						print "</select>\n";
						print "<select name=\"year\">\n";
						for($i = date('Y'); $i <= date('Y') + 10; $i++) print "<option".($i == date('Y', $event['Date']) ? " selected=\"selected\" " : "").">{$i}</option>\n";
						print "</select></td></tr>\n";
						print "<tr><td>Time (HH:MM): </td><td><input type=\"text\" maxlength=\"2\" size=\"1\" name=\"hour\" value=\"".date('g', $event['Date'])."\" />&nbsp;:&nbsp;<input type=\"text\" maxlength=\"2\" size=\"1\" name=\"minute\" value=\"".date('i', $event['Date'])."\" />\n<select name=\"ap\">\n<option".(date('a', $event['Date']) == "am" ? " selected=\"selected\"" : "").">AM</option>\n<option".(date('a', $event['Date']) == "pm" ? " selected=\"selected\"" : "").">PM</option>\n</select></td></tr>";
						print "<tr><td></td><td><br /><input type=\"submit\" value=\"Change Event\" /> <input type=\"reset\" value=\"Reset\" /></td></tr>\n";
						print "</table>\n</form>";
					}else{
						$id = fixArr($_GET, "eventID");
						foreach($_POST as $k => $data){
							$_POST[$k] = trim($data);
							if(empty($_POST[$k])) throwErr("editEvent", "A field was left empty. Please make sure all fields are filled out.", "&eventID={$id}");
						}
						$eventName = trim(fixArr($_POST, "eventName"));
						$eventDesc = trim(fixArr($_POST, "eventDesc"));
						$eventLoc = fixArr($_POST, "eventLoc");
						$month = fixArr($_POST, "month");
						$day = fixArr($_POST, "day");
						$year = fixArr($_POST, "year");
						if(fixArr($_POST, "hour") > 12) throwErr("editEvent", "There are only 12 hours in a day/night. Select PM if you want to use evening times.", "&eventID={$id}");
						if(fixArr($_POST, "minute") > 59) throwErr("editEvent", "There are only 0-59 minutes in 1 hour.", "&eventID={$id}");
						$time = fixArr($_POST, "hour").":".fixArr($_POST, "minute")." ".fixArr($_POST, "ap");
						$eventTime = strtotime($month." ".$day.", ".$year." ".$time);
						if($eventTime < time()) throwErr("editEvent", "The date you picked is in the past.", "&eventID={$id}");
						$_event->editEvent($id, $eventName, $eventDesc, $eventTime, $eventLoc);
						print "The event was successfully modified.";
					}
				}else{
					throwErr("editEvent", "An event with this ID does not exist.");
				}
			}
			break;
		case("delEvent"):
			$temp->setTab(1, 2);
			$temp->currentPage("Home >> Events >> Delete Event");
			$temp->generate();
			$event = fixArr($_GET, "eventID");
			if(!$event){
				$sql = $db->Execute("SELECT * FROM `monster`.`event` WHERE `Date` > ? AND `Approved` = \"1\" ORDER BY `event`.`Date` ASC", array(time()));
				if($sql->RecordCount() > 0){
					print "<span style=\"color: red;\">".(fixArr($_GET, "error") ? fixArr($_GET, "error")."<br />\n" : "")."</span>\n<b>Events</b>:";
					while($row = $sql->FetchRow()){
						print "<br />\nEvent Name: <a href=\"modEvent.php?action=delEvent&eventID={$row['AutoInc']}\">{$row['Title']}</a><br />\n";
						print "Description: {$row['Description']}<br />\n";
						print "Date: ".date('F jS, Y g:i A', $row['Date'])."<br />\n";
					}
				}
			}else{
				if($data = $_event->getEventByID($event)){
					if(fixArr($_GET, "conf") !== "true"){
						print "Are you SURE you want to delete this event ({$data['Title']})? This cannot be undone.<br />\n<br />\n";
						print "<a href=\"modEvent.php?action=delEvent\">No, take me back to the event selection page.</a><br />\n<br />\n";
						print "<a href=\"modEvent.php?action=delEvent&eventID={$event}&conf=true\">Yes, delete it forever.</a>";
					}else{
						$_event->deleteEvent($event);
						print "The event has been deleted successfully!";
					}
				}else{
					throwErr("delEvent", "An event with this ID does not exist.");
				}
			}
			break;
		case("modEvent"):
			$temp->setTab(1, 3);
			$temp->currentPage("Home >> Events >> Moderate Events");
			$temp->generate();
			$event = fixArr($_GET, "eventID");
			if(!$event){
				$sql = $db->Execute("SELECT * FROM `monster`.`event` WHERE `Approved` = \"0\" ORDER BY `event`.`Date` ASC");
				if($sql->RecordCount() > 0){
					print "<b>Events pending approval</b>:";
					while($row = $sql->FetchRow()){
						print "<br />\nEvent Name: <a href=\"modEvent.php?action=modEvent&eventID={$row['AutoInc']}\">{$row['Title']}</a><br />\n";
						print "Description: {$row['Description']}<br />\n";
						print "Date: ".date('F jS, Y g:i A', $row['Date'])."<br />\n";
					}
				}else{
					print "There are currently no events to moderate.";
				}
			}else{
				if($data = $_event->getEventByID($event)){
					if(fixArr($_GET, "conf") === "true"){
						$_event->approveEvent($event);
						print "The event has been approved.";
					}else if(fixArr($_GET, "conf") === "false"){
						if(!fixArr($_GET, "doubleConf")){
							print "Are you SURE you want to deny this event ({$data['Title']})? This will delete it forever.<br />\n<br />\n";
							print "<a href=\"modEvent.php?action=modEvent&eventID={$event}&conf=false&doubleConf=true\">Yes, delete this event.</a><br />\n<br />\n";
							print "<a href=\"modEvent.php?action=modEvent&eventID={$event}\">No, don't delete it.</a>";
						}else{
							$_event->declineEvent($event);
							print "The event has been declined.";
						}
					}else{
						print "Do you want to approve or deny this event? ({$data['Title']})<br />\n<br />\n";
						print "<a href=\"modEvent.php?action=modEvent&eventID={$event}&conf=true\">I want to approve this event.</a><br />\n<br />\n";
						print "<a href=\"modEvent.php?action=modEvent&eventID={$event}&conf=false\">I want to DENY this event.</a>";
					}
				}else{
					print change_site("modEvent.php?action=modEvent");
				}
			}
			break;
	}
?>